package com.yanqu.road.dao.impl.tradewar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.tradewar.UserCrossTradeWarTeam;
import com.yanqu.road.entity.tradewar.UserTradeWarTeam;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserCrossTradeWarTeamDaoImpl extends TempDataObjectDao<UserCrossTradeWarTeam> {

	public UserCrossTradeWarTeam getUserTradeWarTeam(long userId) {
		String sql = "select * from t_u_cross_trade_war_team where userId=?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserCrossTradeWarTeam data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserCrossTradeWarTeam data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_cross_trade_war_team(userId,teamData,teamBuyBuffData) VALUES(?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getPatronsTeamListString());
			call.setString(i++, data.getBuyBuffTimesListString());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserCrossTradeWarTeam data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_cross_trade_war_team SET teamData=?,teamBuyBuffData=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setString(i++, data.getPatronsTeamListString());
			call.setString(i++, data.getBuyBuffTimesListString());
			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserCrossTradeWarTeam parseData(ResultSet rs) throws SQLException {
		UserCrossTradeWarTeam data = new UserCrossTradeWarTeam();
		data.setUserId(rs.getLong("userId"));
		String value = rs.getString("teamData");
		if(StringUtils.isNullOrEmpty(value)){
			value = "";
		}
		data.setPatronsTeamListString(value);
		data.setPatronsTeamList(value);
		value = rs.getString("teamBuyBuffData");
		if(StringUtils.isNullOrEmpty(value)){
			value = "";
		}
		data.setBuyBuffTimesListString(value);
		data.setBuyBuffTimesList(value);
		return data;
	}
}
